<?php
namespace Common\Models;
use Common\BaseModel;
use Common\Common;
use Phalcon\Paginator\Adapter\Model as PaginatorModel;
class Notices extends BaseModel
{
	public $res;
	public $expire;
    public function initialize()
    {
        parent::initialize();
        $this->setSource('f_notices');
    }

    public function get_list($user_info){
        $noticesCateDb = new NoticesCate();
        $cate_list = $noticesCateDb->find()->toarray();
        foreach($cate_list as $key=>$value){
            $cate_list[$key]['child'] = $this->command_noread($value['id'],$user_info);
        }
        return $cate_list;
    }




    //获取未读推荐的公告
    public function command_noread($cid,$user_info){
        $FNoticesUsedDb = new NoticesUsed();
        $is_read_used = $FNoticesUsedDb->find(array(
            'conditions'=>"type<>-1",
            'columns'=>"n_id",
        ))->toarray();
        if($is_read_used){
            $ids = implode(",",array_column($is_read_used,'n_id'));
        }else{
            $ids="-1";
        }
        $get_list = $this->find(array(
            'conditions'=>"type={$cid} AND id not in($ids)",
            'columns'=>"id,title,abstract,ids,created_at",
            'order'=>"created_at desc"
        ))->toarray();
        $materialDb = new Material();
        foreach($get_list as $key=>$value){
            $get_list[$key]['is_read'] = $FNoticesUsedDb->is_read($user_info['id'],$cid);
            $get_list[$key]['turn_time'] = Common::format_date($value['created_at']);
            $material = $materialDb->getListByIds($value['ids'],$user_info);
            $get_list[$key]['router'] = isset($material[0]) ? $material[0] : (Object)null;
        }
        return $get_list;
    }





    /**
     * @公告列表
     * @return mixed
     */
    public function noticelist(){
        $conditions = 'Notices.published = 1';
        $phql = "SELECT Notices.id,Notices.title,Notices.abstract as summary,Notices.file_id,Notices.created_at,Notices.updated_at,Notices.published,Notices.filepath
                FROM Notices
                where  ".$conditions;
        $rows = $this->modelsManager->executeQuery($phql);
        return $rows;
    }

    /**
     * 根据类型获取公告列表
     */
    public function findlist($type,$data=[]){
        $size  = isset($data['page_size']) ? $data['page_size'] : 10;
        $page  = isset($data['page']) ? ($data['page'] - 1) * $size : 0;
        $condition= "type in({$type})";
        if(isset($data['recommend']) && !empty($data['recommend'])){
            $condition .= ' AND recommend=1';
        }
        $list = $this->find(array(
            $condition,
            'columns'=>'id,title,abstract,created_at,filepath,type,ids',
            "limit"=>$size,
            "offset"=>$page,
        ))->toarray();
        return $list;
    }



    /**
     * 获取用户没有读取的公告
     */
    public function no_read_notices_list($ids,$data=[]){
        $condition = "id not in({$ids})";
        if(isset($data['type']) || !empty($data['type'])){
            $condition .= " AND type in ({$data['type']})";
        }
        if(isset($data['recommend']) && !empty($data['recommend'])){
            $condition .= " AND recommend={$data['recommend']}";
        }
        $list = $this->find(array(
            $condition,
            'columns'=>'id,title,abstract,created_at,filepath,type,ids'
        ))->toarray();
        foreach($list as $key=>$value){
            $list[$key]['is_read']= (isset($data['is_read'])&& !empty($data['is_read'])) ? '1' : '0';
            $list[$key]['turn_time'] = Common::format_date($value['created_at']);
        }
        return $list;
    }





    public function get_by_id($id){
        $info = $this->findFirstById($id);
        if($info){
            return $info->toarray();
        }else{
            return false;
        }
    }


    /**
     * 根据推荐获取公告
     * @param $id
     * @return bool
     */
    public function get_by_recommend(){
        $condition="recommend=1";
        $res=$this->findFirst($condition);
        if($res){
            $result = $res->toarray();
            $data['title'] = $result['title'];
            $data['h5url'] = 'http://api.jiruanos.com/invite/noticedetail?id='.$result['id'];
            return $data;
        }else{
            return null;
        }
    }


    /**
     * 读取公告
     */
    public function noticesIdUser($id,$user_id){
        $conditions="user_id={$user_id} AND n_id={$id}";

        $FNoticesUsedDb = new NoticesUsed();
        $res = $FNoticesUsedDb->findFirst(array(
            $conditions
        ));
        if($res){
            return ['status'=>true,'msg'=>'该记录用户已读'];
        }
        $FNoticesDb = new Notices();
        $FNotices = $FNoticesDb->findFirstById($id);
        if(!$FNotices){
            return ['status'=>false,'msg'=>'没有找到公告'];
        }
        $FNoticesUsedDb = new NoticesUsed();
        $FNoticesUsedDb->user_id = $user_id;
        $FNoticesUsedDb->created_at = date("Y-m-d H:i:s");
        $FNoticesUsedDb->n_id = $id;
        $FNoticesUsedDb->type = $FNotices->type;
        if($FNoticesUsedDb->save()){
            return ['status'=>true,'msg'=>'已读取'];
        }else{
            return ['status'=>false,'msg'=>'读取失败'];
        }
    }


    /**
     * 获取用户没有读取的公告条数
     */
    public function notices_no_read_count($user_id,$type){
        $conditions = "id>0 ";
        if($type == -1){
            $conditions .= "AND type=-1";
        }else{
            $conditions .= "AND type<>-1";
        }
        $all_count = $this->count(array(        //获取所有的记录
            $conditions,
        ));
        $conditions .= " AND user_id={$user_id}";
        $FNoticesUsedDb = new NoticesUsed();
        $read_count = $FNoticesUsedDb->count(array(
            $conditions,
        ));
        $no_read_count = $all_count - $read_count;
        return $no_read_count <= 0 ? 0 : $no_read_count;
    }




}
